<?php
/**
 *      [迷你云] (C)2009-2012 南京恒为网络科技.
 *   软件仅供研究与学习使用，如需商用，请访问www.miniyun.cn获得授权
 * 
 */
?>
<?php


class MAPIController
extends MApplicationComponent
implements MIController
{
    private $_common_uri = null;
    public  static $namespace = "api";
    
    public function invoke($uri=null)
    {
                                $url_manager = new MUrlManager();
        $parse_array = $url_manager->parseActionFromUrl();
        if ($parse_array === false)
        {
            throw new MException(FXP::t('{class} do not call an action',
            array('{class}'=>get_class($this))));
        }

        $action = $parse_array["action"];
        $this->_common_uri = $parse_array["uri"];
        self::$namespace   = "api.{$action}";
                                if ($action != "info") {
            $filter = new MUserFilter();
            $filter->invoke($this->_common_uri);
                                                $space_filter = new MActionFilter();
            $space_filter->action = $action;
            $space_filter->invoke($this->_common_uri);
        }
        $this->$action();
                                MAccessLogs::createAccessLogs(self::$namespace, FXP_BEGIN_TIME, microtime(true));
    }
    
    
    private function info() {
        $response = array();
        $response['status']      = SYSTEM_STATUS;
        $response['appname']     = APP_NAME;
        $response['canregister'] = CAN_REGISTER;
        $response['defaultsize'] = DEFAULT_SIZE;
        
        $mOption = new MOptions();
        $retReg = $mOption->queryMeta("user_create_url");
        
        if ($retReg){
            $regurl = $mOption->option_value;
            $response['regurl'] = $regurl;
        }
        
        echo json_encode($response);
    }
    
    
    
    private function oauth()
    {
        $oauth_controller = new MOauthController();
        $path = explode('?', $this->_common_uri);
        $parts = array_slice(explode('/', $path[0]), 1);
        if (count($parts) <= 1)
        {
            throw new MFileopsException(FXP::t('{class} do not call an action',
                                    array('{class}'=>get_class($this))));
        }
        $part = $parts[1];
        if ($pos = strpos($part, '?'))
        {
            $part = substr($part, 0, $pos);
        }
        self::$namespace     .= ".{$part}";
        $oauth_controller -> invoke($this->_common_uri);
    }
    
    
    private function device()
    {
        self::$namespace     .= ".unmount";
        $unmount_controller = new MUnmountController();
        $unmount_controller -> invoke();
    }

    
    private function account()
    {
        $account_controller = null;
        $path = explode('?', $this->_common_uri);
        $parts = array_slice(explode('/', $path[0]), 2);
        if ($parts[0] === "info") {
            self::$namespace    .= ".info";
            $account_controller = new MAccountInfoController();
        }
        $account_controller->invoke();
    }


    
    private function fileops()
    {
        $fileops_controller = null;
        $parts = array_slice(explode('/', $this->_common_uri), 2);
        if (count($parts) < 1)
        {
            throw new MFileopsException(FXP::t('{class} do not call an action',
                                    array('{class}'=>get_class($this))));
        }
        $parts = $parts[0];
        if ($pos = strpos($parts, '?'))
        {
            $parts = substr($parts, 0, $pos);
        }
        if ($parts === "copy") {
            $fileops_controller = new MCopyController();
        }
        elseif ($parts === "create_folder") {
            $fileops_controller = new MCreateFolderController();
        }
        elseif ($parts === "delete") {
            $fileops_controller = new MDeleteController();
        }
        elseif ($parts === "move") {
            $fileops_controller = new MMoveController();
        }
        else {
            throw new MFileopsException(FXP::t('{class} do not call an action',
                                    array('{class}'=>get_class($this))));
        }
        self::$namespace     .= ".{$parts}";
        $fileops_controller->invoke();
    }
    
    
    private function metadata()
    {
        $metadata_controller = new MMetadataController();
        $metadata_controller -> invoke($this->_common_uri);
    }

    
    private  function  files() {
        $files_controller = null;
                                if (@$_SERVER["REQUEST_METHOD"] === "POST") {
            $files_controller = new MFilePostController();
            self::$namespace  .= ".post";
        }
        elseif (@$_SERVER["REQUEST_METHOD"] === "GET") {
            self::$namespace  .= ".get";
            $files_controller = new MFileGetController();
        }
        $files_controller->invoke($this->_common_uri);
    }
    
    
    private  function  files_put() {
        $files_controller = new MFilePutController();
        $files_controller->invoke($this->_common_uri);
    }
    
    
    private function shares()
    {
        $shares_controller = new MSharesController();
        $shares_controller->invoke($this->_common_uri);
    }
    
    
    private function medias() {
        $medias_controller = new MMediasController();
        $medias_controller->invoke($this->_common_uri);
    }
    
    
    private function restore()
    {
        $restore_controller = new MRestoreController();
        $restore_controller->invoke($this->_common_uri);
    }
    
    
    private function search()
    {
        $search_controller = new MSearchController();
        $search_controller->invoke($this->_common_uri);
    }
    
    
    private function thumbnails() {
        $thumbnails = new MThumbnailsController();
        $thumbnails->invoke($this->_common_uri);
    }
    
    
    private function revisions() {
        $revisions = new MRevisionsController();
        $revisions->invoke($this->_common_uri);
    }
    
    
    private function version_check() {
        $version_check = new MVersionCheckController();
        $version_check->invoke();
    }
    
    
    private function events() {
        $events = null;
        $path = explode('?', $this->_common_uri);
        $parts = array_slice(explode('/', $path[0]), 2);
        if ($parts[0] === "list") {
            self::$namespace    .= ".list";
            $events = new MDownloadEventController();
        }
        $events->invoke();
    }
    
    
    
    private function files_sec() {
        $files_controller = new MFilesecController();
        $files_controller->invoke($this->_common_uri);
    }
    
    
    private function adapter() {
        $adapter_controller = new MAdapterController();
        $adapter_controller->invoke();
    }
    
    
    public function handleException($exception)
    {
        $code = $exception->getCode();
        if (empty($code)){
            $code = 403;
        }
        
        if ($exception instanceof MAuthorizationException)
        {
            header("HTTP/1.1 ".$code." ". $exception->getMessage());
            echo $exception->getMessage();
        }

        return $this->displayException($exception);
    }
}
?>